package com.pzhu.user.controller.manage;

import com.pzhu.base.common.utils.result.PageData;
import com.pzhu.base.common.utils.result.Result;
import com.pzhu.base.common.utils.result.Results;
import com.pzhu.user.domain.ro.MenuCreateRO;
import com.pzhu.user.domain.ro.MenuSearchRO;
import com.pzhu.user.domain.ro.MenuUpdateRO;
import com.pzhu.user.domain.vo.MenuSearchVO;
import com.pzhu.user.domain.vo.MenuVO;
import com.pzhu.user.service.impl.MenuService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/api/management/menus")
@RequiredArgsConstructor
@Validated
public class MenuManageController {

    private final MenuService menuService;

    /**
     * 列表
     *
     * @param menuSearchRo 菜单搜索要求
     * @return {@link Result }<{@link Collection }<{@link MenuSearchVO }>>
     */
    @GetMapping
    public Result<Collection<MenuSearchVO>> list(MenuSearchRO menuSearchRo) {
        PageData<MenuSearchVO> pageData = menuService.search(menuSearchRo);
        return Results.page(pageData);
    }

    /**
     * 信息
     *
     * @param menuId 菜单编号
     * @return {@link Result }<{@link MenuVO }>
     */
    @GetMapping("/{menuId}")
    public Result<MenuVO> info(@PathVariable Long menuId) {
        MenuVO result = menuService.info(menuId);
        return Results.success(result);
    }

    /**
     * 创造
     *
     * @param menuCreateRo 菜单创建需求
     * @return {@link Result }<{@link Long }>
     */
    @PostMapping
    public Result<Long> create(@Valid @RequestBody MenuCreateRO menuCreateRo) {
        Long menuId = menuService.save(menuCreateRo);
        return Results.success(menuId);
    }

    /**
     * 更新
     *
     * @param menuId 菜单编号
     * @param menuUpdateRo 菜单更新要求
     * @return {@link Result }<{@link Boolean }>
     */@PutMapping("/{menuId}")
    public Result<Boolean> update(@PathVariable Long menuId, @Valid @RequestBody MenuUpdateRO menuUpdateRo) {
        boolean result = menuService.update(menuId, menuUpdateRo);
        return Results.success(result);
    }

    /**
     * 删除
     *
     * @param menuId 菜单编号
     * @return {@link Result }<{@link Boolean }>
     */@DeleteMapping("/{menuId}")
    public Result<Boolean> delete(@PathVariable Long menuId) {
        boolean result = menuService.removeById(menuId);
        return Results.success(result);
    }

    /**
     * 批量删除
     *
     * @param menuIds 菜单 ID
     * @return {@link Result }<{@link Boolean }>
     */@DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@NotBlank(message = "菜单id不能为空") String menuIds) {
        List<Long> menuIdList =
                Arrays.stream(menuIds.split(",")).map(Long::valueOf).toList();
        boolean result = menuService.removeByIds(menuIdList);
        return Results.success(result);
    }
}
